package com.zxl.yjb.fragment.home.order.send;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.zxl.yjb.R;
import com.zxl.yjb.activity.home.order.CommentActivity;
import com.zxl.yjb.activity.home.order.OrderDetailActivity;
import com.zxl.yjb.activity.home.user.UserSpaceActivity;
import com.zxl.yjb.adapter.OrderSendAdapter;
import com.zxl.yjb.app.Const;
import com.zxl.yjb.base.LazyFragment;
import com.zxl.yjb.bean.LoginSucessBean;
import com.zxl.yjb.bean.OrderSendBean;
import com.zxl.yjb.http.manager.DataManager;
import com.zxl.yjb.http.observer.ObserverOnNextListener;
import com.zxl.yjb.http.observer.ProgressObserver;
import com.zxl.yjb.utils.DisplayUtil;
import com.zxl.yjb.utils.SaveBeanUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;

public abstract class OrderSendBaseFragment extends LazyFragment {

    @BindView(R.id.rv_order)
    RecyclerView rv_order;
    @BindView(R.id.sm_order)
    SmartRefreshLayout sm_order;

    private DataManager dataManager;

    abstract int setOrderStatus();

    abstract int setPayStatus();


    private int page = 1;
    private int count = 10;
    private int order_status = 0;
    private int pay_status = 0;

    private RecyclerView.LayoutManager layoutManager;
    private ArrayList<OrderSendBean> orderSendBeanList = new ArrayList<>();
    private OrderSendAdapter orderSendAdapter;
    private LoginSucessBean loginSucessBean;

    @Override
    protected void lazyLoad() {

    }


    @Override
    public void onResume() {
        super.onResume();
        page = 1;
        getData();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_order;
    }

    @Override
    protected void initView(View view, Bundle savedInstanceState) {
        loginSucessBean = (LoginSucessBean) SaveBeanUtil.getBean(getActivity(), Const.VIPINFO);
        order_status = setOrderStatus();
        pay_status = setPayStatus();
        layoutManager = new LinearLayoutManager(getActivity());
        rv_order.setLayoutManager(layoutManager);
        orderSendAdapter = new OrderSendAdapter(R.layout.item_order_send, orderSendBeanList, order_status, pay_status);
        rv_order.setAdapter(orderSendAdapter);

        orderSendAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {

                if (order_status == 3 && pay_status == 1) {//失效单
                    return;
                }
                Intent intent = new Intent(getActivity(), OrderDetailActivity.class);
                intent.putExtra("id", orderSendBeanList.get(position).getOrder_sn())
                        .putExtra("order_status", order_status)
                        .putExtra("pay_status", pay_status)
                        .putExtra("from", "send_list");
                startActivity(intent);
            }
        });
        orderSendAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                OrderSendBean orderSendBean = (OrderSendBean) adapter.getItem(position);
                if (view.getId() == R.id.btn_comment) {
                    String responseId = orderSendBean.getResponse_id() + "";
                    if (TextUtils.isEmpty(responseId))
                        return;
                    startActivity(new Intent(getContext(), CommentActivity.class)
                            .putExtra("friend_id", responseId)
                            .putExtra("friend_img", orderSendBean.getFace_img())//这个头像要改
                            .putExtra("title", orderSendBean.getCat_type_name() + "_" + orderSendBean.getServer_name())
                            .putExtra("time", orderSendBean.getEnd_time())
                            .putExtra("from", "send_list")
                            .putExtra("order_sn", orderSendBean.getOrder_sn()));//这里全部时发单人对接单人的评论
                }
                else if (view.getId() == R.id.iv_head_img)
                {
                    Intent intent = new Intent(getContext(), UserSpaceActivity.class);
                    if ((order_status == 3&&pay_status == 1)/*||orderSendBean.getRequest_id() == 0*/)
                    {
                        intent.putExtra("uid",loginSucessBean.getUid()+"");
                    }
                    else if((order_status == 1&&pay_status == 1)||(order_status == 2&&pay_status == 1))
                    {
                        intent.putExtra("uid",orderSendBean.getResponse_id());
                    }

                    startActivity(intent);
                }

            }
        });

        orderSendAdapter.setOnItemLongClickListener(new BaseQuickAdapter.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(BaseQuickAdapter adapter, View view, final int position) {

                if (order_status == 3 && pay_status == 1)
                {
                    DisplayUtil.diaplayDialog(getActivity(), "提示", "确定要删除此信息吗？", "取消", "删除", null,new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            del(orderSendBeanList.get(position).getOrder_id()+"",position);
                        }
                    });
                }
                return true;
            }
        });

        sm_order.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                page = 1;
                getData();
                sm_order.setNoMoreData(false);
                sm_order.finishRefresh(5000);
            }
        }).setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                page++;
                getData();
                sm_order.finishRefresh(5000);
            }
        });


    }

    @Override
    protected void main() {

    }


    public void getData() {
        dataManager = new DataManager();
        Map<String, String> map = new HashMap<>();
        map.put("page", page + "");
        map.put("count", count + "");
        map.put("order_status", order_status + "");
        map.put("pay_status", pay_status + "");
        ObserverOnNextListener<ArrayList<OrderSendBean>> listener = new ObserverOnNextListener<ArrayList<OrderSendBean>>() {
            @Override
            public void onNext(ArrayList<OrderSendBean> orderSendBeans) {
                if (page == 1)
                    orderSendBeanList.clear();
                orderSendBeanList.addAll(orderSendBeans);
                orderSendAdapter.notifyDataSetChanged();
                sm_order.finishRefresh();
                sm_order.finishLoadMore();
                if (orderSendBeans.size() < count) {
                    sm_order.setNoMoreData(true);//将不会再次触发加载更多事件
                }
            }
        };
        dataManager.yflists(new ProgressObserver<ArrayList<OrderSendBean>>(listener, getActivity()), map);

    }


    private void del(String id,final int position)
    {
        dataManager = new DataManager();
        Map<String, String> map = new HashMap<>();
        map.put("order_id", id);
        ObserverOnNextListener<Object> listener = new ObserverOnNextListener<Object>() {
            @Override
            public void onNext(Object o) {
                orderSendBeanList.remove(position);
                orderSendAdapter.notifyDataSetChanged();
//                orderSendAdapter.remove(position);
            }
        };
        dataManager.del_invalidOrder(new ProgressObserver<Object>(listener, getActivity()), map);
    }


}
